import { fileURLToPath } from 'url'
import {defineConfig, loadEnv} from 'vite'
import type { ConfigEnv, UserConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
// 依赖引入
import AutoImport from "unplugin-auto-import/vite";
import Icons from "unplugin-icons/vite";
import Components from "unplugin-vue-components/vite";
import IconsResolver from "unplugin-icons/resolver";
import ElementPlus from "unplugin-element-plus/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import {viteMockServe} from "vite-plugin-mock"
export default defineConfig(({mode}: ConfigEnv):UserConfig => {
    const root = process.cwd()
    const env = loadEnv(mode,root)
    return {
        // 项目根目录
        root,
        // 项目部署的基础路径
        base:"/",
        publicDir: fileURLToPath(new URL("./public",import.meta.url)), //无需处理的静态资源位置
        assetsInclude: fileURLToPath(new URL("./src/assets",import.meta.url)),//需要处理的静态资源位置
        plugins: [
            vue(),
            vueJsx(),
            viteMockServe({
                mockPath: "mock", //
                enable: true // 本地环境是否开启mock
            }),
            // 开启ElementPlus自动引入CSS
            ElementPlus({}),
            // 自动引入组件及ICON
            AutoImport({
                resolvers: [IconsResolver(), ElementPlusResolver()],
                dts: fileURLToPath(new URL("./types/auto-imports.d.ts", import.meta.url)),
            }),
            // 自动注册组件
            Components({
                resolvers: [IconsResolver(), ElementPlusResolver()],
                dts: fileURLToPath(new URL("./types/components.d.ts", import.meta.url)),
            }),
            // 自动安装图标
            Icons({
                autoInstall: true,
            }),
        ],
        server: {
            host: true,
            port: 9000,
            open: true,
            cors: true,
            proxy: {
                [env.VITE_APP_API_BASEURL]: {
                    target: "http://localhost:9000",
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/api/,"")
                },
                [env.VITE_APP_MOCK_BASeURL]: {
                    target: "http://localhost:9000",
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/mock\/api/,"")
                }
            }

        },
        build: {
            // 关闭sourcemap 报错不会映射到源码
            sourcemap: false,
            // 打包大小超出400kb 提示警告
            chunkSizeWarningLimit: 400,
            rollupOptions: {
                input: {
                    index: fileURLToPath(new URL("./index.html",import.meta.url))
                },
                // 静态资源打包
                output: {
                    format: "esm",
                    chunkFileNames: "static/js/[name]-[hash].js",
                    entryFileNames: "static/js/[name]-[hash].js",
                    assetFileNames: "static/[ext]/[name]-[hash].[ext]",
                }
            }
        },
        // 配置别名
        resolve: {
            alias: {
                "@": fileURLToPath(new URL("./src",import.meta.url)),
                "#": fileURLToPath(new URL("./types",import.meta.url))
            }
        }
    }
})

